package fr.imag.ufrima.ima.project.ui.main;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.apache.log4j.Logger;

import fr.imag.ufrima.ima.project.core.Image;
import fr.imag.ufrima.ima.project.ui.utils.Utils;

// class to display an ImageIcon on a panel

@SuppressWarnings("serial")
public class ImageDocument extends JPanel implements InternalFrameListener {
	private Logger logger = Logger.getLogger(getClass().getName());
	
	private ImageProject imageProject;

	private Image image;
	private int id;
	private Rectangle selection;
	private boolean isSelectionSet;
	private JInternalFrame frameContainer;

	// load image
	public ImageDocument(int id, String imageFileName, JInternalFrame frameContainer, ImageProject imageProject) {
		this.imageProject = imageProject;
		this.frameContainer = frameContainer;
		
		this.id = id;
		this.image = new Image(imageFileName);
		selection = new Rectangle();
		super.setName(imageFileName);		
	}

	// display image on panel
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(this.getBufferedImage(), 0, 0, null);
		frameContainer.setTitle(this.getTitle());

		Graphics2D g2 = (Graphics2D)g;
		if(isSelectionSet) {
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
		    g2.fill(selection);
		}
		g2.draw(selection);
	}
	
    public void setSelectionFrame(Point start, Point end) {
        selection.setFrameFromDiagonal(start, end);
        repaint();
    }
    
    public void setIsSelectionSet(boolean isSelectionSet) {
    	this.isSelectionSet = isSelectionSet;
    }

	public Dimension getPreferredSize() {
		return new Dimension(this.getBufferedImage().getWidth(), this.getBufferedImage().getHeight());
	}

	public int getId() {
		return id;
	}

	public BufferedImage getBufferedImage() {
		return image.getBufferedImage();
	}

	public String toString() {
		return String.format("ImageDocument[id: %d, filename: %s]", id, super.getName());
	}

	public Image getImage() {
		return image;
	}

	private String getTitle() {
		StringBuilder frameTitle = new StringBuilder();

		if (image.isModified()) {
			frameTitle.append("*");
		}
		frameTitle.append(image.getName());

		return frameTitle.toString();
	}

	@Override
	public void internalFrameActivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		logger.debug(String.format("Internal frame activated."));
		imageProject.getToolsPanel().hideAll();
	}

	@Override
	public void internalFrameClosed(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
		logger.debug(String.format("Internal frame closed."));
		if (!imageProject.isImageDocumentAvailable()) {
			imageProject.imageDocumentIsNotAvailable();
		}
	}

	@Override
	public void internalFrameClosing(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
		logger.debug(String.format("Internal frame closing..."));
		if (image.isModified()) {
			int a = JOptionPane.showOptionDialog(imageProject, "Voulez vous garder l'image? \n \n"+image.getName(), "Garder l'image?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
			logger.debug(String.format("Answer: %s", a));	
			
			if (a == JOptionPane.YES_OPTION) {
				image.saveToFile(image.getName(), Utils.getExtension(image.getName()));
				logger.debug(String.format("Image was saved before closing following user instructions."));		
			}
			else {
				logger.debug(String.format("Image was close without saving following user instructions."));
			}
		}		
	}

	@Override
	public void internalFrameDeactivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void internalFrameDeiconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void internalFrameIconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void internalFrameOpened(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

}